﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class news_ModuleCalendarAdd : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bd();
        }
    }
    /// <summary>
    /// 加载时绑定的数据
    /// </summary>
    private void bd()
    {
        MainQuoteDBModel.MainQuoteDBEntities stocks = DAL.DBManager.GetMainQuoteDbEntities();
        ddlModule.DataSource = stocks.TB_BK.Where(x => x.BK_CODE.StartsWith("002"));
        ddlModule.DataTextField = "bk_name";
        ddlModule.DataValueField = "bk_code";
        ddlModule.DataBind();
    }
    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        CMSModel.ModuleCalendar m = new CMSModel.ModuleCalendar();
        m.hotModule = ddlModule.SelectedItem.ToString();   //热门板块id 
        m.reason = txtReason.Text;      //可关注理由
        m.ModuleId = ddlModule.SelectedValue;          //热门板块
        DateTime dt;
        string date = txtTime.Text;
        DateTime.TryParse(date, out dt);
        m.ctime = dt;   //获取日期
        string stocks = hidSelectValue.Value;
        string stockname = txtStock.Text;
        MainQuoteDBModel.MainQuoteDBEntities s = DAL.DBManager.GetMainQuoteDbEntities();
        //判断是否重复添加热门板块
        if (dal.ModuleCalendar.Any(x => x.ctime == m.ctime))
        {
            ClientScript.RegisterStartupScript(GetType(), "add", "alert('您已添加" + m.ctime.ToShortDateString() + "的热门板块');document.location='ModuleCalendar.aspx'", true);
            return;
        }
        try
        {
            //添加至板块日历
            dal.AddToModuleCalendar(m);
            dal.SaveChanges();
            long id = dal.ModuleCalendar.OrderByDescending(x => x.ctime).FirstOrDefault().id;
            //添加至股票表
            foreach (var x in stocks.Split(','))
            {
                if (!string.IsNullOrEmpty(x))
                {
                    //根据key得到value
                    string y = "";
                    if (s.TB_GGXX.Any(c => c.G_CODE == x))
                    {
                        y = s.TB_GGXX.First(c => c.G_CODE.Contains(x)).G_NAME;
                    }
                    else
                    {
                        y = x;
                    }
                    dal.AddToModuleCalendarStocks(new CMSModel.ModuleCalendarStocks() { stock = x, stockname = y, fId = id });
                }

            }
            //循环添加入板块事件表
            var lstNews = dal.News.Where(x => x.keyword.Contains(m.hotModule) && x.ptime <= DateTime.Now && x.IsEnable == true).Select(x => new { x.title, x.id, x.ptime }).OrderByDescending(x => x.ptime).Take(5).ToList();
            if (lstNews.Count > 0)
            {
                foreach (var n in lstNews)
                {
                    dal.AddToModuleCalendarEvents(new CMSModel.ModuleCalendarEvents() { newsId = n.id, fId = id });
                }
            }
            dal.SaveChanges();
            ClientScript.RegisterStartupScript(GetType(), "add", "alert('添加成功');document.location='ModuleCalendar.aspx'", true);
        }
        catch (Exception)
        {
            ClientScript.RegisterStartupScript(GetType(), "add", "alert('添加失败')", true);
        }
    }
    /// <summary>
    /// 热门板块更新后弹出层可关注股票数据改变
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
    {
        string t = ddlModule.SelectedValue;
        MainQuoteDBModel.MainQuoteDBEntities stocks = DAL.DBManager.GetMainQuoteDbEntities();
        List<MainQuoteDBModel.TB_BK_GG> lstBK_GG = stocks.TB_BK_GG.Where(x => x.BK_CODE.Contains(t)).ToList();
        //循环加入至checkboxlist
        foreach (var bk in lstBK_GG)
        {
            var lst = CommonBll.DicStocks.First(x => x.Key.Contains(bk.GG_CODE));
            var name = lst.Key;
            var value = lst.Value;
            ListItem item = new ListItem(value, name);
            cblStock.Items.Add(item);
        }
    }
}